home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / ans / chap20 / exer2002 / Word.java
Encoding:
Java Source  |  1997-04-20  |  2.2 KB  |  81 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class Word extends Applet {
  5.    private TextArea area;
  6.    private Choice fonts;
  7.    private Choice sizes;
  8.  
  9.    private String currentFont = "Courier";
  10.    private int currentSize = 8;
  11.    private int style = Font.PLAIN;
  12.    private Color currentColor = Color.black;
  13.  
  14.    public void init() {
  15.       setLayout(new BorderLayout());
  16.  
  17.       area = new TextArea(10, 60);
  18.       area.setFont(new Font(currentFont, style, currentSize));
  19.       area.setForeground(currentColor);
  20.       add("Center", area);
  21.  
  22.       Panel p = new Panel();
  23.       p.setLayout(new GridLayout(2,1));
  24.  
  25.       Panel firstRow = new Panel();
  26.       CheckboxGroup group = new CheckboxGroup();
  27.       firstRow.add(new Checkbox("black", group, true));
  28.       firstRow.add(new Checkbox("blue", group, false));
  29.       firstRow.add(new Checkbox("red", group, false));
  30.       p.add(firstRow);
  31.  
  32.       Panel secondRow = new Panel();
  33.       fonts = new Choice();
  34.       fonts.addItem("Courier");
  35.       fonts.addItem("Helvetica");
  36.       fonts.addItem("Times Roman");
  37.       secondRow.add(fonts);
  38.  
  39.       sizes = new Choice();
  40.       sizes.addItem("8");
  41.       sizes.addItem("12");
  42.       sizes.addItem("16");
  43.       secondRow.add(sizes);
  44.  
  45.       p.add(secondRow);
  46.  
  47.       add("South", p);
  48.    }
  49.  
  50.    public boolean action(Event e, Object what) {
  51.       if (e.target instanceof Checkbox) {
  52.          Checkbox c = (Checkbox)(e.target);
  53.          String s = c.getLabel();
  54.          if (s.equals("black"))
  55.             currentColor = Color.black;
  56.          else if (s.equals("blue"))
  57.             currentColor = Color.blue;
  58.          else
  59.             currentColor = Color.red;
  60.       }
  61.  
  62.       else if (e.target == fonts) {
  63.           currentFont = fonts.getSelectedItem();
  64.       }
  65.  
  66.       else if (e.target == sizes) {
  67.           switch (sizes.getSelectedIndex()) {
  68.              case 0: currentSize = 8; break;
  69.              case 1: currentSize = 12; break;
  70.              case 2: currentSize = 16; break;
  71.           }
  72.       }
  73.  
  74.       area.setFont(new Font(currentFont, style, currentSize));
  75.       area.setForeground(currentColor);
  76.  
  77.       return super.action(e, what);
  78.    }
  79.  
  80. }
  81.